import json

from .getPublicData import *
from myApp.models import JobInfo

def getAverged(list):
    result = 0
    for i in list:
        result += i
    return round(result / len(list), 2)

def getPageData():
    return list(educations.keys())

def getExperienceData(defaultEducations):
    hasEmpty = False
    if defaultEducations == "不限":
        jobs = getAllJobs()
    else:
        jobs = JobInfo.objects.filter(educational=defaultEducations)
    WExperiences = {}
    workPeople = {}
    for i in workExperience:
        WExperiences[i] =[]
        workPeople[i] = 0
    for job in jobs:
        for k,v in WExperiences.items():
            if job.workExperience == k:
                if job.internship == '0':
                    WExperiences[k].append(json.loads(job.salary)[1])
                    workPeople[k] += 1

    for k,v in WExperiences.items():
        try:
            WExperiences[k] = getAverged(v)
        except:
            WExperiences[k] = 0

    if len(jobs) == 0:
        hasEmpty = True

    return workExperience,list( WExperiences.values()),list(workPeople.values()),hasEmpty

def getPeopleData():
    jobs = getAllJobs()
    educationData = {}
    for i in jobs:
        if educationData.get(i.educational,-1) == -1:
            educationData[i.educational] = 1
        else:
            educationData[i.educational] += 1
    return list(educationData.keys()),list(educationData.values())